package com.movieproject.daos.imp;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.movieproject.daos.ReviewDAO;
import com.movieproject.dataobjects.Movie;
import com.movieproject.dataobjects.Review;
import com.movieproject.dataobjects.User;

@Service
public class ReviewDAOImp implements ReviewDAO {


private DataSource dataSource;

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	public void insert(Review review) {
		String sql = "INSERT INTO REVIEWS" + "(UserID, MovieID, Score,Review) VALUES (?,?,?,?)";

		Connection conn = null;

		try {
			conn = dataSource.getConnection();
			PreparedStatement pss = conn.prepareStatement(sql);
			pss.setInt(1, review.getUserID());
			pss.setInt(2, review.getMovieID());
			pss.setInt(3, review.getScore());
			pss.setString(4, review.getReview());
			pss.executeUpdate();
			pss.close();

		} catch (SQLException e) {
			throw new RuntimeException(e);

		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		
	}


    @Autowired
    private SessionFactory sessionFactory;
    
    @Override
    public List<Review> getAllReviews() {
	Query query = sessionFactory.getCurrentSession().createQuery("from Review");
	List<?> l = query.list(); // query returns a raw list. 
	ArrayList<Review> reviewList = new ArrayList<Review>();
	for(Object o : l) {	// objects in list must be cast individually
	    reviewList.add((Review) o);
	}
	return reviewList;
    }

    @Override
    public List<Review> getReviewsByMovie(Movie movie) {
	Query query = sessionFactory.getCurrentSession().createQuery("from Review where MovieID = :m");
	query.setParameter("m", movie.getMovieID());
	List<?> l = query.list(); // query returns a raw list. 
	ArrayList<Review> reviewList = new ArrayList<Review>();
	for(Object o : l) {	// objects in list must be cast individually
	    reviewList.add((Review) o);
	}
	return reviewList;
    }

    @Override
    public List<Review> getReviewsByUser(User user) {
	Query query = sessionFactory.getCurrentSession().createQuery("from Review where UserID = :u");
	query.setParameter("u", user.getUserID());
	List<?> l = query.list(); // query returns a raw list. 
	ArrayList<Review> reviewList = new ArrayList<Review>();
	for(Object o : l) {	// objects in list must be cast individually
	    reviewList.add((Review) o);
	}
	return reviewList;
    }

    @Override
    public Review getReview(int reviewID) {
	return (Review) sessionFactory.getCurrentSession().get(Review.class, reviewID);
    }

    @Override
    public void updateReview(Review review) {
	sessionFactory.getCurrentSession().merge(review);
    }

    @Override
    public void deleteReview(Review review) {
	sessionFactory.getCurrentSession().delete(review);
	
    }

}
